package com.desire.service.sys.impl;

import com.desire.bean.sys.Dict;
import com.desire.common.Base.BaseServiceImpl;
import com.desire.mapper.sys.DictMapper;
import com.desire.service.sys.DictService;
import com.desire.utils.MapUtils;
import com.desire.utils.StringUtils;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Service
public class DictServiceImpl extends BaseServiceImpl<Dict, DictMapper> implements DictService {

    @Override
    public int save(Dict mapOrEntiry) {
        //查询当前企业用户下指定父id下的最大编码
        String code = mapper.queryMaxDictCode(mapOrEntiry.getEuid(), mapOrEntiry.getParentid());
        String newcode = null;//新编码
        if (null == code) {
            //说明当前没有编码
            if (StringUtils.isBland(mapOrEntiry.getParentid()) || "1".equals(mapOrEntiry.getParentid())) {
                newcode = "101";
            } else {
                //当前父id下的第一个编码  查询父id对应的编码
                code = mapper.queryDictCode(mapOrEntiry.getParentid());
                newcode = code + "01";
            }
        } else {
            newcode = getNextCode(code);
        }
        mapOrEntiry.setDictcode(newcode);
        return super.save(mapOrEntiry);
    }

    @Override
    public List<Map> queryChild(Map map) {
        return mapper.queryChild(map);
    }

    /**
     * 获取当前编码+1后的新编码
     *
     * @param code
     * @return
     */
    String getNextCode(String code) {
        int len = code.length();
        String num = code.substring(len - 2);
        int newNum = Integer.parseInt(num) + 1;
        String newCode = code.substring(0, len - 2) + String.format("%02d", newNum);
        return newCode;
    }


    @Override
    public List<Map<String, Object>> queryChildByParentId(String pid) {
        List list = mapper.queryChildByParentId(pid);
        Iterator<Map> it = list.iterator();
        while (it.hasNext()) {
            Map map = it.next();
            int rs = MapUtils.getDefaultIntValue(map, "childs", 0);
            boolean flag = true;//默认是子节点
            if (rs > 0) {
                flag = false;//不是子节点
            }
            map.put("isLeaf", flag);
            map.remove("childs");
        }
        return list;
    }

    @Override
    public int delete(Serializable id) {
        // 查询当前字典下是否有子字典
        int i = mapper.queryChidlCount(id);
        if (i > 0) {
            // 有子字典
            return 2;
        }
        return super.delete(id);
    }

    @Override
    public int deleteGroup(String code) {
        return mapper.deleteGroup(code);
    }

    @Override
    public List<Map<String, Object>> queryChildren(Map<String, Object> map) {
        List<Map<String, Object>> list = mapper.queryChildren(map);
        for (Map<String, Object> tmp : list) {
            int childs = MapUtils.getDefaultIntValue(tmp, "childs", 0);
            boolean isLeaf = childs > 0;
            tmp.put("isLeaf", isLeaf);
            tmp.remove("childs");
        }
        return list;
    }
}
